var mysql = require('mysql');
var log = require("./../../util/loginfo").getInstand;
var mysql_config = require("./../../util/mysql_config");

var pool = mysql.createPool({
    connectionLimit: 10000,
    host: mysql_config.host,
    user: mysql_config.user,
    password: mysql_config.password,
    port: mysql_config.port,
    database: 'ym_manage',
    charset: "utf8mb4",
});

//插入新订单
exports.InsertNewOrder = function InsertNewOrder(orderInfo, callback) {
    var sql = 'insert into order_list (orderNO, userId, changeGold, orderStatus,type, beforeGold, afterGold) values (?,?,?,?,?,?,?)';
    var values = [];
    values.push(orderInfo.orderNO);
    values.push(orderInfo.userId);
    values.push(orderInfo.changeGold);
    values.push(orderInfo.orderStatus);
    values.push(orderInfo.type);
    values.push(orderInfo.beforeGold);
    values.push(orderInfo.afterGold);

    pool.getConnection(function (err, connection) {
        connection.query({sql: sql, values: values}, function (err, rows) {
            connection.release();
            if (err) {
                log.err("InsertNewOrder error == ", err);
                callback(0);
            } else {
                callback(1);
            }
        });
        values = [];
    });
};

//插入新订单
exports.UpdateOrder = function UpdateOrder(orderInfo, callback) {
    var sql = 'update order_list set orderStatus = ?, endTime = NOW(), afterGold = ? where userId = ? and orderNO = ?';
    var values = [];
    values.push(orderInfo.orderStatus);
    values.push(orderInfo.afterGold);
    values.push(orderInfo.userId);
    values.push(orderInfo.orderNO);

    pool.getConnection(function (err, connection) {
        connection.query({sql: sql, values: values}, function (err, rows) {
            connection.release();
            if (err) {
                log.err("UpdateOrder error == ", err);
                callback(0);
            } else {
                callback(1);
            }
        });
        values = [];
    });
};